package ${bao}.${serviceImplPackage};

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${bao}.${entityPackage}.${entity};
import ${bao}.${mapperPackage}.${entity}Mapper;
import ${bao}.${servicePackage}.${entity}Service;
import com.utils.common.ResponseEnum;
import com.utils.common.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* 描述: ${table.comment}
* author: ${author}
* date: ${date}
*/

@Service
public class ${entity}ServiceImpl implements ${entity}Service {

    private static Logger logger = LoggerFactory.getLogger(${entity}ServiceImpl.class);

    @Autowired
    private ${entity}Mapper ${entity?uncap_first}Mapper ;


    @Override
    public ServerResponse list(${entity} ${entity?uncap_first}) {
        try {
            List<${entity}> jsonObject = ${entity?uncap_first}Mapper.list(${entity?uncap_first});
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR);
        }
    }

    @Override
    public ServerResponse pageList(${entity} ${entity?uncap_first}) {
        Map<String,Object> returnMap=new HashMap<>();
        try {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageSize(${entity?uncap_first}.getPageSize());
            pageInfo.setPageNum(${entity?uncap_first}.getPageNum());
            PageHelper.startPage(pageInfo);
            List<${entity}> list = ${entity?uncap_first}Mapper.list(${entity?uncap_first});
            PageInfo<${entity}> jsonObject=new PageInfo<${entity}>(list);
            returnMap.put("list",jsonObject.getList());
            returnMap.put("pages",jsonObject.getPages());
            returnMap.put("total",jsonObject.getTotal());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(returnMap);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR);
        }
    }

    @Override
    public ServerResponse get(String id) {
        try {
            ${entity} jsonObject = ${entity?uncap_first}Mapper.get(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR);
        }
    }

    @Override
    public ServerResponse add${entity}(${entity} ${entity?uncap_first}) {
        try {
            ${entity?uncap_first}.setId(UUID.randomUUID().toString());
            ${entity?uncap_first}Mapper.add${entity}(${entity?uncap_first});
            return ServerResponse.getInstance().responseEnum(ResponseEnum.ADD_SUCCESS).data(${entity?uncap_first});
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR);
        }
    }

    @Override
    public ServerResponse delete${entity}(String id) {
        try {
            ${entity?uncap_first}Mapper.delete${entity}(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.DELETE_SUCCESS);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.DELETE_ERROR);
        }
    }
    @Override
    public ServerResponse update${entity}(${entity} ${entity?uncap_first}) {
        try{
            ${entity?uncap_first}Mapper.update${entity}(${entity?uncap_first});
            return ServerResponse.getInstance().responseEnum(ResponseEnum.UPDATE_SUCCESS).data(${entity?uncap_first});
        }catch (Exception e){
            logger.error(e.getMessage());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.UPDATE_ERROR);
        }
    }

}
